<?php

namespace app\admin\controller;

use app\admin\controller\Utils\JavaCompatible;
use app\app\model\ProjectBannerModel;
use app\app\model\ProjectGGKTypeModel;
use app\app\model\ProjectModel;
use app\app\model\ProjectRiskModel;
use app\app\model\RandModel;
use app\app\model\SysDictDataModel;
use app\BaseController;
use app\Request;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\db\Where;
use think\helper\Str;


class Project extends BaseController
{
    public function projectQuery()
    {
        $project_type = input('project_type', '');
        $link_project_id = input('link_project_id', '');
        $where = [];
        if ($project_type) {
            $where[] = ['project_type', '=', $project_type];
        }
        if ($link_project_id) {
            $where[] = ['link_project_id', '=', $link_project_id];
        }
        $list = ProjectModel::field('id,project_name')->where($where)->select();
        $this->success('success', $list);
    }

    public function detail()
    {
        $id = input('project_id');
        $info = ProjectModel::find($id);
        $this->success('success', $info);
    }

    public function add()
    {
        $data = $this->request->param();
        $data['id'] = RandModel::randId();
        ProjectModel::create($data);

        $control_type = ProjectGGKTypeModel::select();
        $todo_task_list = array();
        foreach ($control_type as $key => $value) {
            $item['project_id'] = $data['id'];
            $item['ggk_id'] = $value['id'];
            $item['create_time'] = date('Y-m-d h:i:s', time());
            array_push($todo_task_list, $item);
        }

        $res = (new \app\app\model\ProjectGGKModel)->saveAll($todo_task_list, false);
        $this->success('success');
    }

    /**
     * 保存项目
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function save()
    {
        $data = $this->request->param();

        if (empty($data['id'])) {
            $project = new ProjectModel();

            $project->id = RandModel::randId();
        } else {
            $project = ProjectModel::find($data['id']);
        }

        $data = JavaCompatible::snake($data);

        $project->save($data);

        $this->success('success');
    }

    public function update(Request $request)
    {
        $data = $request->param();
        $id = $request->param('id');
        unset($data['id']);
        $result = ProjectModel::where('id', $id)->update($data);
        if ($result !== false) {
            $this->success("保存成功！");
        } else {
            $this->error("保存失败！");
        }
    }

    /**
     * 查询详情
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getInfo()
    {
        $id = input('id');

        $info = ProjectModel::where('id', $id)->find();

        empty($info) && $this->error('未找到符合条件的项目');

        $info = JavaCompatible::camel($info->toArray());

        $this->success('success', $info);
    }

    public function bannerAdd(){
        $data = $this->request->param();
        ProjectBannerModel::create($data);
        $this->success('添加成功');
    }
}
